package com.kongjs.application.events;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.kongjs.application.model.entity.LoginLog;
import com.kongjs.application.service.ILoginLogService;
import jakarta.annotation.Resource;
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.authorization.AuthorizationResult;
import org.springframework.security.authorization.event.AuthorizationDeniedEvent;
import org.springframework.security.authorization.event.AuthorizationGrantedEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.function.Supplier;

@Component
public class AuthenticationEvents {

    @Resource
    private ILoginLogService loginLogService;

    /*
     * 认证成功
     * */
    @EventListener
    public void onSuccess(AuthenticationSuccessEvent success) {
        Authentication authentication = success.getAuthentication();
        String name = authentication.getName();
        WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails)authentication.getDetails();
        String ip = webAuthenticationDetails.getRemoteAddress();
        long timestamp = success.getTimestamp();
    }

    /*
     * 认证失败
     * */
    @EventListener
    public void onFailure(AbstractAuthenticationFailureEvent failure) {
        Authentication authentication = failure.getAuthentication();
        String name = authentication.getName();
        WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails)authentication.getDetails();
        String ip = webAuthenticationDetails.getRemoteAddress();
        long timestamp = failure.getTimestamp();
        AuthenticationException exception = failure.getException();
    }

    /*
     * 授权成功
     * */
    @EventListener
    public void onSuccess(AuthorizationGrantedEvent<Authentication> success) {
        Supplier<Authentication> authenticationSupplier = success.getAuthentication();
        Authentication authentication = authenticationSupplier.get();
        long timestamp = success.getTimestamp();
        AuthorizationResult authorizationResult = success.getAuthorizationResult();
        boolean granted = authorizationResult.isGranted();
    }

    /*
     * 授权失败
     * */
    @EventListener
    public void onFailure(AuthorizationDeniedEvent<Authentication> failure) {
        Supplier<Authentication> authenticationSupplier = failure.getAuthentication();
        Authentication authentication = authenticationSupplier.get();
        long timestamp = failure.getTimestamp();
        AuthorizationResult authorizationResult = failure.getAuthorizationResult();
        boolean granted = authorizationResult.isGranted();
    }

}
